package code.google.jcontainer.invoke;

import java.lang.reflect.Method;

/**
 * MethodInvocationHandler invokes method by reflect, normally, this should be the last InvocationHandler of the chain. 
 * 
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 */
public class MethodInvocationHandler implements InvocationHandler {

    /**
     * invoke method by reflect
     *
     * @param invocation
     * @throws Exception
     */
    public void chainInvoke(final Invocation invocation) throws Exception {
        // need to handle the registered Interceptors
        Object targetComponent = invocation.getTargetComponent();
        Method targetMethod = invocation.getTargetMethod();
        // need to set accessible to true to ensure method can be accessed by reflection
        targetMethod.setAccessible(true);
        Object result = targetMethod.invoke(targetComponent, invocation.getParameters());
        invocation.setResult(result);
    }

    public void chainReturn(Invocation invocation) throws Exception {
        // do nothing
    }

}
